package com.justgame.project.cloud.common.util;

import com.justgame.project.cloud.base.entity.BaseEntity;
import com.justgame.project.cloud.base.entity.BaseTable;
import com.justgame.project.cloud.common.exception.Exc;
import lombok.SneakyThrows;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
 * 增强【 Assert 】功能
 *
 * @author ZhouWeiBo
 */
public class AssertKit extends Assert {

    /**
     * 期望字符传为空，若反之则抛出异常
     *
     * @param object       校验对象
     * @param throwMessage 报错信息
     */
    public static void nonLength(String object, String throwMessage) {
        if (StringUtils.hasLength(object)) {
            throw new Exc(throwMessage);
        }
    }

    /**
     * 期望布尔值为空，若反之则抛出异常
     *
     * @param isFalse      结果
     * @param throwMessage 报错信息
     */
    public static void isFalse(boolean isFalse, String throwMessage) {
        if (isFalse) {
            throw new Exc(throwMessage);
        }
    }

    /**
     * 期望对象等值，若反之则抛出异常
     *
     * @param a            校验对象a
     * @param b            校验对象b
     * @param throwMessage 报错信息
     */
    public static void isEquals(Object a, Object b, String throwMessage) {
        if (!Objects.equals(a, b)) {
            throw new Exc(throwMessage);
        }
    }

    /**
     * 期望对象不等，若反之则抛出异常
     *
     * @param a            校验对象a
     * @param b            校验对象b
     * @param throwMessage 报错信息
     */
    public static void notEquals(Object a, Object b, String throwMessage) {
        if (Objects.equals(a, b)) {
            throw new Exc(throwMessage);
        }
    }

    /**
     * 期望字符串等长度等于指定长度，若反之则抛出异常
     *
     * @param str          字符串
     * @param length       指定长度
     * @param throwMessage 报错信息
     */
    public static void eqLength(String str, int length, String throwMessage) {
        if ((str == null && length > 0) || (str != null && str.length() != length)) {
            throw new Exc(throwMessage);
        }
    }

    public static void lengthRange(String str, int min, int max, String throwMessage) {
        if ((str == null && min > 0) || (str != null && (str.length() < min || str.length() > max))) {
            throw new Exc(throwMessage);
        }
    }

    /**
     * 期望对象不为空，并且id值不为空并且不为-1，若反之则抛出异常
     *
     * @param entity 身份/对象
     * @param <T>    对象类继承于BaseEntity
     */
    public static <T extends BaseTable> void checkId(T entity, String throwMessage) {
        if (entity == null || entity.getId() == null || entity.getId() == -1) {
            throw new Exc(throwMessage);
        }
    }


    /**
     * 期望对象不为空，并且id值不为空并且不为-1，若反之则抛出异常
     *
     * @param entity 身份/对象
     * @param <T>    对象类继承于BaseEntity
     */
    public static void checkId(Object entity, String throwMessage) {
        if (!ObjUtil.checkId(entity)) {
            throw new Exc(throwMessage);
        }
    }

    /**
     * 期望对象不为空，并且id值不为空并且不为-1，若反之则抛出异常
     *
     * @param id 用户id
     */
    @SneakyThrows
    public static void checkId(Long id, String throwMessage) {
        if (id == null || id == -1) {
            throw new Exc(throwMessage);
        }
    }

    /**
     * 期望值不为0 ( 非零 )，若反之则抛出异常
     *
     * @param number       期望值
     * @param throwMessage 抛出的异常信息
     */
    public static <T extends Number> void nonZero(T number, String throwMessage) {
        if (number != null && number.intValue() == 0) {
            throw new Exc(throwMessage);
        }
    }

    /**
     * 期望值等于0 ( 等于零 )，若反之则抛出异常
     *
     * @param number       期望值
     * @param throwMessage 抛出的异常信息
     */
    public static <T extends Number> void isZero(T number, String throwMessage) {
        if (number != null && number.intValue() != 0) {
            throw new Exc(throwMessage);
        }
    }

}
